﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NITGEN.SDK.NBioBSP;
using System.Data;
using System.IO;
using System.Windows.Threading;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for EmployeeAttendence.xaml
    /// </summary>
    public partial class EmployeeAttendence : UserControl
    {
        NBioAPI m_NBioAPI;
        NBioAPI.IndexSearch m_IndexSearch;
        byte[] fingerData;
        string fingerPrintTextData = string.Empty;
        MemoryStream fingerPrint;
        private DispatcherTimer timer;
       
        public EmployeeAttendence()
        {
            InitializeComponent();
            m_NBioAPI = new NBioAPI();
            m_IndexSearch = new NBioAPI.IndexSearch(m_NBioAPI);
            uint ret = m_IndexSearch.InitEngine();
            if (ret != NBioAPI.Error.NONE)
            {
                m_NBioAPI.CloseDevice(NBioAPI.Type.DEVICE_ID.AUTO);
                return;
            }
            
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer1_Tick;
            timer.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblMessage.Content = "";
            callAgain();
        }

        //private void DisplayErrorMsg(uint ret)
        //{
        //    MessageBox.Show(NBioAPI.Error.GetErrorDescription(ret) + " [" + ret.ToString() + "]", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
        //}

        private void convertFIRInToBinary(NBioAPI.Type.HFIR hNewFIR)
        {
            uint ret = NBioAPI.Error.NONE;
            NBioAPI.Type.FIR GetFirInfo;

            ret = m_NBioAPI.GetFIRFromHandle(hNewFIR, out GetFirInfo);


            if (ret == NBioAPI.Error.NONE)
            {
                fingerPrint = new MemoryStream();
                StreamWriter sw = new StreamWriter(fingerPrint);
                sw.Write(GetFirInfo);
                fingerData = new byte[GetFirInfo.Data.Length];
                fingerData = GetFirInfo.Data;
            }

            NBioAPI.Type.FIR_TEXTENCODE GetFirTextInfo;
            ret = m_NBioAPI.GetTextFIRFromHandle(hNewFIR, out GetFirTextInfo, true);
            if (ret == NBioAPI.Error.NONE)
            {
                string s = GetFirTextInfo.TextFIR.ToString();
                //MessageBox.Show(s.Length.ToString() );
                fingerPrintTextData = s;


            }

        }

        private NBioAPI.Type.FIR_TEXTENCODE convertFIRInToText(NBioAPI.Type.HFIR hNewFIR)
        {
            uint ret = NBioAPI.Error.NONE;
            NBioAPI.Type.FIR_TEXTENCODE GetFirTextInfo;
            ret = m_NBioAPI.GetTextFIRFromHandle(hNewFIR, out GetFirTextInfo, true);
            return GetFirTextInfo;

        }

        private void callAgain()
        {
            NBioAPI.Type.HFIR hCapturedFIR;
            NBioAPI.Type.FIR_TEXTENCODE hMyCapturedFIR;
            NBioAPI.Type.FIR_TEXTENCODE hRegisterdFIR;

            m_NBioAPI.OpenDevice(NBioAPI.Type.DEVICE_ID.AUTO);
            uint ret = m_NBioAPI.Capture(out hCapturedFIR);
            if (ret != NBioAPI.Error.NONE)
            {
                //DisplayErrorMsg(ret);
                m_NBioAPI.CloseDevice(NBioAPI.Type.DEVICE_ID.AUTO);
                return;
            }
            convertFIRInToBinary(hCapturedFIR);

            hMyCapturedFIR = new NBioAPI.Type.FIR_TEXTENCODE();
            hMyCapturedFIR = convertFIRInToText(hCapturedFIR);
            m_NBioAPI.CloseDevice(NBioAPI.Type.DEVICE_ID.AUTO);


            DataTable dtEmpBioMetric = new DataTable();
            dtEmpBioMetric = AbhiruchiDAL.getEmployeeBiometricInfo();

            hRegisterdFIR = new NBioAPI.Type.FIR_TEXTENCODE();

            NBioAPI.Type.FIR_PAYLOAD myPayload = new NBioAPI.Type.FIR_PAYLOAD();
            m_NBioAPI.OpenDevice(NBioAPI.Type.DEVICE_ID.AUTO);
            bool IsMatched = false;
            bool result;

            foreach (DataRow dr in dtEmpBioMetric.Rows)
            {
                hRegisterdFIR.TextFIR = dr[4].ToString();
                ret = m_NBioAPI.VerifyMatch(hMyCapturedFIR, hRegisterdFIR, out result, myPayload);
                if (ret != NBioAPI.Error.NONE)
                {
                    m_NBioAPI.CloseDevice(NBioAPI.Type.DEVICE_ID.AUTO);
                    return;
                }
                else
                {
                    if (result == true)
                    {
                        IsMatched = true;
                        //lblMessage.Content = "Attendence Registered Successfully";
                        //lblMessage.Foreground = Brushes.Gray;
                        mannageAttendence(Convert.ToInt32(dr[0]),dr[5].ToString());
                        //MessageBox.Show("Welcome " + dr[5].ToString());
                        m_NBioAPI.CloseDevice(NBioAPI.Type.DEVICE_ID.AUTO);
                        return;
                    }
                }
            }
            if (IsMatched == false)
            {
                //MessageBox.Show("------ Sorry! Finger Print Not Matched-------");
                lblMessage.Content = "------ Sorry! Finger Print Not Matched-------";
                lblMessage.Foreground = Brushes.Red;
            }
            m_NBioAPI.CloseDevice(NBioAPI.Type.DEVICE_ID.AUTO);

        }

        private void mannageAttendence(int empId,string empName)
        {
            DataTable dtAttStatus = new DataTable();
            dtAttStatus = AbhiruchiDAL.GetEmployeeAttendenceStatus(empId);
            int noOfRows = dtAttStatus.Rows.Count;

            DataTable dtCheckEmpEnable = new DataTable();
            dtCheckEmpEnable = AbhiruchiDAL.GetEmployeeEnableStatus(empId);
            if (dtCheckEmpEnable.Rows[0][0].ToString() == "1")
            {
                if (noOfRows == 0)//no Record Present means Punched In first time for that day
                {
                    lblMessage.Content = "Welcome " + empName + " Come In";
                    lblMessage.Foreground = Brushes.Gray;

                    AbhiruchiDAL.SetEmployeeAttendence(empId);
                }
                else if (dtAttStatus.Rows[noOfRows - 1][0].ToString() == "1")//Means Punched Out
                {
                    lblMessage.Content = empName + " Went Out";
                    lblMessage.Foreground = Brushes.Gray;

                    AbhiruchiDAL.UpdateEmployeeAttendence(empId);
                }
                else if (dtAttStatus.Rows[noOfRows - 1][0].ToString() == "0")//Means Punched In not first time for that day
                {
                    lblMessage.Content = "Welcome " + empName + " Come In Again";
                    lblMessage.Foreground = Brushes.Gray;

                    AbhiruchiDAL.SetEmployeeAttendence(empId);
                }
            }
            else if (dtCheckEmpEnable.Rows[0][0].ToString() == "0")
            {
                lblMessage.Content =  empName + " Is InActive";
            }
        }
    }
}
